package com.newy.service.misc;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.newy.core.AppException;
import com.newy.core.BaseService;
import com.newy.dao.misc.FqaPostDao;
import com.newy.dao.misc.FqaTypeDao;
import com.newy.dao.misc.FqaValuationDao;
import com.newy.domain.misc.FqaPost;
import com.newy.domain.misc.FqaType;
import com.newy.domain.misc.FqaValuation;

/**
 * 
 * @author linzongxue 2012-2-19
 *
 */
@Service
@Transactional
public class FqaService extends BaseService {
	@Resource private FqaTypeDao typeDao;
	@Resource private FqaPostDao postDao;
	@Resource private FqaValuationDao valDao;
	
	@Transactional(readOnly=true)
	public FqaType getType(Long id){
		return typeDao.get(id);
	}
	
	public void saveType(FqaType type){
		if (!typeDao.isPropsUnique(type, "parentId", "name"))
			throw new AppException("类型名称(%s)已存在", type.getName());
		if (type.getId() != null && type.getId() == type.getParentId())
			throw new AppException("父类型不能是自身");
		//设置父类型Id路径
		if (type.getId() == null){//添加的情况
			if (type.getParentId() == null || type.getParentId() == 0){
				type.setParentIdPath("/");
			}
			else{
				FqaType parent = typeDao.get(type.getParentId());
				type.setParentIdPath(parent.getParentIdPath() + parent.getId() + "/");
			}
		}
		else{ //修改的情况
			FqaType old = typeDao.get(type.getId());
			if (type.getParentId() != old.getParentId()){
				FqaType newParent = typeDao.get(type.getParentId());
				type.setParentIdPath(newParent != null ? newParent.getParentIdPath() + newParent.getId() + "/" : "/");
				//修改机构下所有子机构的parentIdPath
				for (FqaType t : typeDao.getAllChildren(type.getId())){
					String newPath = t.getParentIdPath().replaceFirst("^" + old.getParentIdPath(), type.getParentIdPath());
					t.setParentIdPath(newPath);
					typeDao.save(t);
				}
			}
			else{ //防止parentIdPath被无意修改
				type.setParentIdPath(old.getParentIdPath());
			}
		}
		typeDao.save(type);
	}
	
	/**
	 * 删除类型，包括所有子类。
	 * 当类型下存在FAQ内容时不允许删除
	 */
	public void deleteType(Long id){
		List<FqaType> all = new ArrayList<FqaType>();
		all.add(typeDao.load(id));
		all.addAll(typeDao.getAllChildren(id));
		for (FqaType type : all){
			if (postDao.getCountOfType(type.getId()) > 0)
				throw new AppException("类型(%s)下面还有FQA记录，不能被删除", type.getName());
			typeDao.delete(type);
		}
	}

	@Transactional(readOnly=true)
	public List<FqaType> getTypeChildren(Long parentId){
		return typeDao.getChildren(parentId);
	}
	
	/*====  FQA内容 ====*/
	@Transactional(readOnly=true)
	public FqaPost getPost(Long id){
		return postDao.get(id);
	}
	
	public void savePost(FqaPost post){
		FqaType type = typeDao.get(post.getTypeId());
		if (type == null){
			throw new AppException("保存失败：FQA类型不正确。");
		}
		postDao.save(post);
	}
	
	/**
	 * 删除FQA内容，同时删除所有评价
	 */
	public void deletePost(Long id){
		for (FqaValuation val : valDao.getAllOfPost(id)){
			valDao.delete(val);
		}
		postDao.delete(id);
	}

	@Transactional(readOnly=true)
	public List<FqaPost> getAllPostsOfType(Long typeId){
		if (typeId == null || typeId == 0){
			return postDao.getAll();
		}
		FqaType type = typeDao.get(typeId);
		return postDao.getAllOfType(type);
	}
}
